package com.springboot.chatroom.service;


import com.springboot.chatroom.mapper.MessageMapper;
import com.springboot.chatroom.model.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: jiume
 * Date: 2025-04-16
 * Time: 16:31
 */
@Service
public class MessageService {

    @Autowired
    private MessageMapper messageMapper;

    public String getLastMessageBySessionId(Integer sessionId) {
        return messageMapper.getLastMessageBySessionId(sessionId);
    }

    public List<Message> getHistoryMessageBySessionId(Integer sessionId) {
        List<Message> messageList = messageMapper.getHistoryMessageBySessionId(sessionId);
        // 进行升序排序，为了让前端更好的拼接页面
        Collections.reverse(messageList);
        return messageList;
    }
}
